﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace XFFSM
{
    public class Context
    {

        #region 字段

        private RuntimeFSMController _runtimeFSMController;

        public List<FSMStateNodeData> SelectNodes = new List<FSMStateNodeData>();

        public FSMTransitionData selectTransition;

        public bool isPreviewTransition = false;
        public FSMStateNodeData fromState = null;
        public FSMStateNodeData hoverState = null;

        private FSMController _FSMController;

        #endregion

        #region 属性
        public float ZoomFactor { get; set; } = 0.3f;

        public Vector2 DragOffset { get; set; }

        public RuntimeFSMController RuntimeFSMController{
            get {
                RuntimeFSMController runtimeFSM = GetFSMController();
                if (runtimeFSM != null && runtimeFSM != _runtimeFSMController) {
                    Reset();
                    _runtimeFSMController = runtimeFSM;
                }
                return _runtimeFSMController;
            }
        }

        public FSMController FSMController {
            get {
                if (Application.isPlaying) {
                    GameObject obj = Selection.activeObject as GameObject;
                    if (obj != null && obj.GetComponent<FSMController>() != null) 
                    {
                        FSMController[] controllers = obj.GetComponents<FSMController>();

                        foreach (var item in controllers)
                        {
                            if (item.ShowInEditorWindow) {
                                _FSMController = item;
                                return _FSMController;
                            }
                        }

                        _FSMController = controllers[0];
                    }
                }

                return _FSMController;
            }
        }

        #endregion

        #region 方法
        public void Reset() {
            // 重置缩放和偏移
            this.ZoomFactor = 0.3f;
            this.DragOffset = Vector2.zero;
        }

        // 获取RuntimeFSMController
        private RuntimeFSMController GetFSMController() {
            RuntimeFSMController controller = Selection.activeObject as RuntimeFSMController;

            if (controller != null) {
                return controller;
            }

            GameObject obj = Selection.activeObject as GameObject;

            if (obj != null && obj.GetComponent<FSMController>() != null) 
            {

                FSMController[] controllers = obj.GetComponents<FSMController>();

                foreach (var item in controllers)
                {
                    if (item.ShowInEditorWindow)
                    {
                        controller = item.RuntimeFSMController;
                        return controller;
                    }
                }
                
                controller = controllers[0].RuntimeFSMController;
            }
            return controller;
        }

        public void ClearSelections()
        {
            // 如果不是 InspectorWindow 此时清空 Inspector
            this.SelectNodes.Clear();
            selectTransition = null;
            Selection.activeObject = this.RuntimeFSMController;
        }

        public void StartPreviewTransition(FSMStateNodeData node) {
            this.fromState = node;
            this.isPreviewTransition = true;
        }

        public void StopPreviewTransition() {
            this.fromState = null;
            this.isPreviewTransition = false;
        }

        #endregion
    }

}

